package top.ddandang.blog.service.impl;
import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import top.ddandang.blog.model.pojo.Draft;
import top.ddandang.blog.mapper.DraftMapper;
import top.ddandang.blog.model.vo.DraftVo;
import top.ddandang.blog.service.DraftService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import top.ddandang.blog.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author D
 * @since 2020-06-26
 */
@Service
@Slf4j
public class DraftServiceImpl extends ServiceImpl<DraftMapper, Draft> implements DraftService {

    @Resource
    private DraftMapper draftMapper;

    @Resource
    private RedisUtil redisUtil;

    /**
     * 缓存中草稿箱的Key值
     */
    private final String listDraftKey = "list:listDraft";

    @Override
    public int insertDraft(DraftVo draftVo) {
        Draft draft = new Draft()
                .setArticleId(draftVo.getArticleId())
                .setTitle(draftVo.getTitle())
                .setContentHtml(draftVo.getContentHtml())
                .setContentMd(draftVo.getContentMd());
        while (true) {
            if(draftMapper.insert(draft) == 1) {
                break;
            }
        }
        //删除缓存
        redisUtil.del(listDraftKey);
        return draft.getId();
    }

    @Override
    public void updateDraft(DraftVo draftVo) {
        Draft draft = new Draft()
                .setId(draftVo.getId())
                .setTitle(draftVo.getTitle())
                .setContentHtml(draftVo.getContentHtml())
                .setContentMd(draftVo.getContentMd());
        //如果文章的id不为空，则代表改文章已经发布然后修改的时候进行存入草稿箱
        if(draftVo.getArticleId() != null) {
            draft.setArticleId(draftVo.getArticleId());
        }
        draftMapper.update(draft, new UpdateWrapper<Draft>()
        .eq("id", draft.getId()));
        //删除缓存
        redisUtil.del(listDraftKey);
    }

    @Override
    public Draft getDraftById(Integer id) {
        return draftMapper.selectById(id);
    }

    @Override
    public List<Draft> listDraft() {
        List<Draft> drafts = new ArrayList<>();
        if(redisUtil.hasKey(listDraftKey)) {
            Map<Object, Object> map = redisUtil.hmget(listDraftKey);
            for(Map.Entry<Object, Object> draft:map.entrySet()){
                drafts.add((Draft)draft.getValue());
            }
        }else {
            drafts = draftMapper.selectList(null);
            Map< String,Object> map = new HashMap<>();
            for (Draft draft : drafts){
                map.put(draft.getId().toString(),draft);
            }
            redisUtil.hmset(listDraftKey,map);
        }
        return drafts;
    }

    @Override
    public void deleteDraft(Integer id) {
        draftMapper.deleteById(id);
        //删除缓存
        redisUtil.del(listDraftKey);
    }

    @Override
    public int judgeDraft(Integer articleId) {
        Draft draft = draftMapper.selectOne(new QueryWrapper<Draft>()
        .eq("article_id", articleId)
        .select("id"));
        int id = 0;
        if(draft != null) {
            id = draft.getId();
        }
        log.error(id + "草稿箱id");
        return id;
    }
}
